//
//  YBCreatCustomGroupViewController.m
//  GongXinDangJian
//
//  Created by apple on 17/12/5.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YBCreatCustomGroupViewController.h"
#import "LPCostomLabelAndTextFieldAndImageView.h"
#import "YBWaterLayout.h"
#import "YBNormalWaterCollectionViewCell.h"
#define CELLID @"YBNormalWaterCollectionViewCell"
#import "LCustomAlertView.h"

#import "YBAddCustomGroupSelectPeopleViewController.h"

@interface YBCreatCustomGroupViewController ()<UICollectionViewDelegate , UICollectionViewDataSource ,YBWaterLayoutDelegate , LCustomAlertDelegate , YBAddCustomGroupSelectPeopleViewControllerDelegate>
@property (nonatomic , strong) LPCostomLabelAndTextFieldAndImageView * nameTextField;

@property (nonatomic , strong) UILabel * peopleNumberLabel;

@property (nonatomic , strong) UIButton * addButton;

@property (nonatomic , strong) UICollectionView * collectionView;

@property (nonatomic , strong) NSMutableArray<YBPeopleInfoModel *> * dataArray;

@property (nonatomic , strong) UIButton * sureButton;

@property (nonatomic , assign) NSInteger currentDeleteIndex;

@property (nonatomic , strong) JCAlertView * jcAlertView;
@end

@implementation YBCreatCustomGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.currentDeleteIndex = 0;
    [self ybInitUserInterface];
    if (self.isChange) {
        [self ybInitDataSource];
    }
}


-(void)ybInitUserInterface{
    self.view.backgroundColor = [UIColor themeGrayColor];
    [self.view addSubview:self.nameTextField];
    [self.view addSubview:self.peopleNumberLabel];
    [self.view addSubview:self.addButton];
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.sureButton];
}


-(void)ybInitDataSource{
    if (self.transformationArray.count > 0) {
        self.dataArray = [self.transformationArray mutableCopy];
        self.peopleNumberLabel.text = [NSString stringWithFormat:@"小组成员(%ld):" , self.dataArray.count];
    }
    self.nameTextField.secondTextField.text = self.model.NAME;
}


-(void)option_addButton:(UIButton *)sender{
    YBAddCustomGroupSelectPeopleViewController * selectVC = [[YBAddCustomGroupSelectPeopleViewController alloc]init];
    selectVC.delegate = self;
    [self.navigationController pushViewController:selectVC animated:YES];
}


-(void)option_ensureButton:(UIButton *)sender{
    if (self.nameTextField.secondTextField.text.length == 0) {
        [YJProgressHUD showSuccess:@"请输入小组名称" inview:self.view];
        return;
    }
    if (self.dataArray.count == 0) {
        [YJProgressHUD showSuccess:@"请选择小组成员" inview:self.view];
        return;
    }
    [YJProgressHUD showMessage:@"提交中..." duration:10 view:[UIApplication sharedApplication].keyWindow];
    NSMutableArray * tempArray = [NSMutableArray array];
    for (YBPeopleInfoModel * model in self.dataArray) {
        NSDictionary * dic = @{@"ACCOUNT_ID" : [ToolClass returnNoNullStringWithString:model.ID] , @"F_DEPT_NAME" : [ToolClass returnNoNullStringWithString:model.F_DEPT_NAME]};
//        NSString * jsonString = [ToolClass getjsonStringFromDictionary:dic];
//        [tempArray addObject:[ToolClass returnNoNullStringWithString:jsonString]];
        [tempArray addObject:dic];
    }
    [YBRequestManager request_changeOrAddCustomGroupWithGroupID:[ToolClass returnNoNullStringWithString:self.model.ID] deptName:self.nameTextField.secondTextField.text users:[tempArray copy] successBlock:^(id object) {
        [YJProgressHUD hide];
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        else{
            [YJProgressHUD showSuccess:@"提交错误" inview:self.view];
        }
        
    } failBlock:^(id object) {
        [YJProgressHUD hide];
        [YJProgressHUD showSuccess:@"网络错误" inview:self.view];
    }];
}




-(void)option_flowLayoutCellDeleteButton:(UIButton *)sender{
    
    NSInteger index = sender.tag - 1000;
    self.currentDeleteIndex = index;
    YBPeopleInfoModel * tempModel = self.dataArray[index];
    LCustomAlertView *alert = [[LCustomAlertView alloc] initWithFrame:self.view.bounds alertMessage:[NSString stringWithFormat:@"是否删除 %@-%@" , [ToolClass returnNoNullStringWithString:tempModel.F_DEPT_NAME] , [ToolClass returnNoNullStringWithString:tempModel.NAME]] titlesArray:@[@"取消",@"确定"]];
    alert.delegate = self;
    self.jcAlertView = [[JCAlertView alloc]initWithCustomView:alert dismissWhenTouchedBackground:YES];
    [self.jcAlertView show];
}

-(void)clickEnsureButtonWithCurrentSelectArray:(NSArray *)currentSelectArray
{
    if (currentSelectArray.count == 0) {
        return;
    }
    NSMutableArray * userIdArray = [NSMutableArray array];
    for (YBPeopleInfoModel * tempModel in self.dataArray) {
        [userIdArray addObject:tempModel.ID];
    }
    
    for (YBPeopleInfoModel * addModel in currentSelectArray) {
        if (![userIdArray containsObject:addModel.ID]) {
            [self.dataArray addObject:addModel];
        }
    }
    [self.collectionView reloadData];
    self.peopleNumberLabel.text = [NSString stringWithFormat:@"小组成员(%ld):" , self.dataArray.count];
}


#pragma mark - LCustomAlertDelegate
- (void)lCustomAlertWithView:(LCustomAlertView *)view index:(NSInteger)index{
    if (index == 1) {
        /**  确认  */
        [self.dataArray removeObjectAtIndex:self.currentDeleteIndex];
        [self.collectionView reloadData];
        self.peopleNumberLabel.text = [NSString stringWithFormat:@"小组成员(%ld):" , self.dataArray.count];
    }
    [self.jcAlertView dismissWithCompletion:nil];
}




-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YBNormalWaterCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELLID forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    cell.deleteButton.tag = 1000 + indexPath.row;
    [cell.deleteButton addTarget:self action:@selector(option_flowLayoutCellDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}


-(CGFloat)WaterfallFlowLayout:(YBWaterLayout *)layout widthForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YBPeopleInfoModel * model = self.dataArray[indexPath.row];
    return model.nameWidth + ItemWidth * 60;
}




-(LPCostomLabelAndTextFieldAndImageView *)nameTextField
{
    if (!_nameTextField) {
        _nameTextField = [[LPCostomLabelAndTextFieldAndImageView alloc]initWithFrame:CGRectMake(0, navHeight + ItemHeight * 20, [UIScreen zj_screenWith], ItemHeight * 160) title:@"小组名称:" placehold:@"" imageString:@""];
        _nameTextField.secondTextField.backgroundColor = [UIColor whiteColor];
        _nameTextField.secondTextField.font = LYPFONT(18);
        _nameTextField.secondTextField.userInteractionEnabled = YES;
    }
    return _nameTextField;
}

-(UILabel *)peopleNumberLabel
{
    if (!_peopleNumberLabel) {
        _peopleNumberLabel = [ToolClass creatLabelWithFrame:CGRectMake(ItemWidth * 24, ItemHeight * 200 + navHeight, [UIScreen zj_screenWith] - ItemWidth * 48, ItemHeight * 40) font:15 textColor:[UIColor textBlackColor] text:@"小组成员(0):"];
    }
    return _peopleNumberLabel;
}


-(UIButton *)addButton
{
    if (!_addButton) {
        _addButton = [ToolClass createButtonWithFrame:CGRectMake(ItemWidth * 120, ItemHeight * 260 + navHeight, [UIScreen zj_screenWith] - ItemWidth * 240, ItemHeight * 88) text:@"添加人员" textColor:[UIColor textBlackColor] imageName:@"" font:18];
        _addButton.backgroundColor = [UIColor whiteColor];
        _addButton.clipsToBounds = YES;
        _addButton.layer.cornerRadius = 3;
        [_addButton addTarget:self action:@selector(option_addButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}


-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        YBWaterLayout * layout = [[YBWaterLayout alloc]init];
        layout.minimumLineSpacing = ItemHeight * 1;
        layout.minimumInteritemSpacing = ItemWidth * 10;
        layout.sectionInset = UIEdgeInsetsMake(ItemHeight * 5, ItemWidth * 10, ItemHeight * 5 , ItemWidth * 10);
        layout.itemHeight = 75 * ItemHeight;
        layout.delegate = self;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(ItemWidth * 24, ItemHeight * 400 + navHeight, [UIScreen zj_screenWith] - ItemWidth * 48, ItemHeight * 400) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor themeGrayColor];
        [_collectionView registerClass:[YBNormalWaterCollectionViewCell class] forCellWithReuseIdentifier:CELLID];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.allowsSelection = NO;
    }
    return _collectionView;
}


-(NSMutableArray<YBPeopleInfoModel *> *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(UIButton *)sureButton
{
    if (!_sureButton) {
        _sureButton = [ToolClass createButtonWithFrame:CGRectMake(0, [UIScreen zj_screenHeight] - ItemHeight * 100, [UIScreen zj_screenWith], ItemHeight * 100) text:@"确定" textColor:[UIColor whiteColor] imageName:@"" font:16];
        _sureButton.backgroundColor = [UIColor themeColor];
        [_sureButton addTarget:self action:@selector(option_ensureButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureButton;
}


@end
